import { getH5UserInfo, getCardType } from '@/api/user.js'
import { setToken, getToken, getWxInfo, setWxInfo } from '@/api/wxLogin.js'
const state = {
  userInfo: {

  },
  token: getToken(),
  WxInfo: getWxInfo(),
  isLogin: false,
  // 身份证类型
  cardTypeList: []
}
const mutations = {
  SET_USER_INFO(state, userInfo) {
    state.userInfo = userInfo
  },
  SET_LOGING(state, isLogin) {
    state.isLogin = isLogin
  },
  // 挂载接口返回的微信&用户数据 token
  setAllUserInfo(state, data) {
    const { token, ckfuser, wechatuser } = data
    state.WxInfo = wechatuser
    state.token = token
    state.userInfo = ckfuser
    // 在cookies里挂载token wxinfo
    setToken(token)
    setWxInfo(wechatuser)
  },
  // 挂载接口返回的微信&用户数据 token
  setH5UserInfo(state, data) {
    const { token, ckfUserbase } = data
    state.WxInfo = {}
    state.token = token
    state.userInfo = ckfUserbase
    // 在cookies里挂载token wxinfo
    setToken(token)
    setWxInfo({})
  },
  // 挂载身份证类型
  SET_CARD_TYPE(state, cardTypeList) {
    state.cardTypeList = cardTypeList
  }
}
// const actions = {
//   // 已有token的情况下挂在用户信息
//   async setUserInfo({ commit }) {
//     console.log('setUserInfo1')
//     const { data: userInfo } = await getH5UserInfo()
//     console.log('setUserInfo2')
//     commit('SET_USER_INFO', userInfo)
//     // 身份证类型获取
//     console.log('getCardType1')
//     const { data: cardTypeList } = await getCardType()
//     console.log('getCardType2')
//     commit('SET_CARD_TYPE', cardTypeList.map(v => ({ text: v.dictLabel, value: v.dictValue })))
//     commit('SET_LOGING', true)

//   },
// }
const actions = {
  // 已有token的情况下挂在用户信息
  async setUserInfo({ state, commit }) {
    commit('SET_USER_INFO', state.userInfo)
    // 身份证类型获取
    console.log('SET_USER_INFO',state.userInfo)
    const { data: cardTypeList } = await getCardType()
    commit('SET_CARD_TYPE', cardTypeList.map(v => ({ text: v.dictLabel, value: v.dictValue })))
    console.log('SET_CARD_TYPE',state.cardTypeList)
    commit('SET_LOGING', true)
    console.log('SET_LOGING',state.isLogin)
  },
  // 已有token的情况下挂在用户信息
  async setUserBaseInfo({ state, commit }) {
    const {data: userdata} = await getH5UserInfo();
    console.log('userdata',userdata.ckfUserbase)
    commit('SET_USER_INFO', userdata.ckfUserbase)
    // 身份证类型获取
    console.log('SET_USER_INFO',userdata.ckfUserbase)
    const { data: cardTypeList } = await getCardType()
    commit('SET_CARD_TYPE', cardTypeList.map(v => ({ text: v.dictLabel, value: v.dictValue })))
    console.log('SET_CARD_TYPE',state.cardTypeList)
    commit('SET_LOGING', true)
    console.log('SET_LOGING',state.isLogin)
  },
}
export default {
  state,
  mutations,
  actions
}

// https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx7f28cbec848e4d93&redirect_uri=http%3A%2F%2Fabcccdddede.vaiwan.com%2F&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect